package com.better.common.utils;

import com.better.common.utils.redis.RedisUtils;
import com.better.common.utils.serc.Cryptos;
import com.better.common.utils.serc.Digests;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * 常用加密算法工具类
 * Created by Administrator on 2017/11/13.
 */
public class EncryptUtils {
    public static final String HASH_ALGORITHM = "SHA-1";
    public static final int HASH_INTERATIONS = 1024;
    public static final int SALT_SIZE = 8;

    /**
     * 生成安全的密码，生成随机的16位salt并经过1024次 sha-1 hash
     */
    public static String entryptPassword(String plainPassword) {
        String plain = Encodes.unescapeHtml(plainPassword);
        byte[] salt = Digests.generateSalt(SALT_SIZE);
        byte[] hashPassword = Digests.sha1(plain.getBytes(), salt, HASH_INTERATIONS);
        return Encodes.encodeHex(salt) + Encodes.encodeHex(hashPassword);
    }

    /**
     * 验证密码
     *
     * @param plainPassword 明文密码
     * @param password      密文密码
     * @return 验证成功返回true
     */
    public static boolean validatePassword(String plainPassword, String password) {
        String plain = Encodes.unescapeHtml(plainPassword);
        byte[] salt = Encodes.decodeHex(password.substring(0, 16));
        byte[] hashPassword = Digests.sha1(plain.getBytes(), salt, HASH_INTERATIONS);
        return password.equals(Encodes.encodeHex(salt) + Encodes.encodeHex(hashPassword));
    }

    /**
     * @author:
     * @date :2018/2/2
     */

    public static boolean validatePassWord(String sh1Password, String password, String userName) throws NoSuchAlgorithmException {
        String ssoLoginAuthRandomKey = "sso:userloginauth:random";
        RedisUtils redisUtils = (RedisUtils) SpringContextUtils.getBean("redisUtils");
        String r = redisUtils.getStr(ssoLoginAuthRandomKey + ":" + userName, false);
        if (StringUtils.isEmpty(r))
            return false;
        return sh1Password.equals(Encodes.md5(password + r));
    }

    public static void main(String[] args) throws NoSuchAlgorithmException {
  /*      String plain = Encodes.unescapeHtml("zxy111111");
        byte[] salt = Encodes.decodeHex("785f5a2a4bbf57764c3d768632e0352a3835a66bbfceb6f26d53591e".substring(0, 16));
        byte[] hashPassword = Digests.sha1(plain.getBytes(), salt, 3);
        String ss = Encodes.encodeHex(salt) + Encodes.encodeHex(hashPassword);
        System.out.println(ss);*/
        System.out.println( Encodes.decodeBase64String("enh5MTExMTEx"));
        String plain = Encodes.unescapeHtml("zxy111111");
        byte[] salt = Encodes.decodeHex("785f5a2a4bbf57764c3d768632e0352a3835a66bbfceb6f26d53591e".substring(0, 16));
        String ss=Encodes.encodeHex(salt);
        byte[] salt111= Encodes.decodeHex(ss);
        byte[] pass1 = Encodes.decodeHex("785f5a2a4bbf57764c3d768632e0352a3835a66bbfceb6f26d53591e".substring(16));
        byte[] hashPassword = Digests.sha1(plain.getBytes(), salt, HASH_INTERATIONS);
        System.out.println("hashPassword"+hashPassword);

        System.out.println("根据盐值加密的密码："+Encodes.encodeHex(salt) + Encodes.encodeHex(hashPassword));
      /*  String xx = "fa29f685eae223a46cba7b63a8a071e1";
        String dd = Encodes.md5("785f5a2a4bbf57764c3d768632e0352a3835a66bbfceb6f26d53591e" + "pls8ohr3");
        MessageDigest md = MessageDigest.getInstance("MD5");
        System.out.println(dd);*/
    }
}
